package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.algorithm.performance.SoundCardEvaluator;
import cn.edu.nju.software.cdiy.bean.BRD_SOUND;
import cn.edu.nju.software.cdiy.bean.Brand;
import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.HardwareSet;
import cn.edu.nju.software.cdiy.bean.Interface;
import cn.edu.nju.software.cdiy.bean.MB_Interface;
import cn.edu.nju.software.cdiy.bean.MainBoard;
import cn.edu.nju.software.cdiy.bean.SC_IOINTF;
import cn.edu.nju.software.cdiy.bean.SoundBox;
import cn.edu.nju.software.cdiy.bean.SoundCard;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.SoundCardDao;
import cn.edu.nju.software.cdiy.util.Changer;
import cn.edu.nju.software.cdiy.util.Constant;
import cn.edu.nju.software.cdiy.util.FilterItem;
import cn.edu.nju.software.cdiy.util.Item;
import cn.edu.nju.software.cdiy.util.Pager;
import cn.edu.nju.software.cdiy.util.SortByHotAsc;
import cn.edu.nju.software.cdiy.util.SortType;
import cn.edu.nju.software.cdiy.util.SortWay;

public class SoundCardDaoImpl implements SoundCardDao {

	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<SoundCard> getSoundCardList(int page, SortType s,
			HardwareSet hs, SortWay sw) {
		// System.out.println("进入daoimpl getmblist");

		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<SoundCard> list = null;

		// 第page页的第minNum条记录到maxNum条记录
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		switch (s) {
		case PRICE:
			if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY OPRICE ASC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY OPRICE DESC";
			list = sess.createQuery(sql).list();
			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY PERFORMANCE DESC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY PERFORMANCE ASC";
			list = sess.createQuery(sql).list();
			break;
		case W_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY WSCORE DESC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY WSCORE ASC";
			list = sess.createQuery(sql).list();
			break;
		case Z_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY ZSCORE DESC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY ZSCORE ASC";
			list = sess.createQuery(sql).list();
			break;
		case NEW:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
			list = sess.createQuery(sql).list();

			break;
		case HOT:
			sql = "FROM SoundCard WHERE STATE=882";
			list = sess.createQuery(sql).list();
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				Collections.sort(list);
			else {
				Collections.sort(list, new SortByHotAsc());
			}
			break;
		default:
			break;

		}
		list = filterList(hs, list, minNum, maxNum);

		return list;
	}

	private List<SoundCard> filterList(HardwareSet hs, List<SoundCard> list,
			int min, int max) {

		/*
		 * 分别由各部件的filter对待检测的部件表进行筛选 目前只有一个条件可以用来筛选 MainBoard
		 */
		list = uniFilter(hs, list);
		// 最终根据页数的要求返回list
		List<SoundCard> returnList = new Pager<SoundCard>().getPageList(list,
				min, max);
		return returnList;
	}

	private List<SoundCard> MainBoardfilter(HardwareSet hs, List<SoundCard> l) {
		/*
		 * 主板对声卡的限制 声卡总线接口标准
		 */
		if (hs.getMainBoard() != null) {
			MainBoard mb = hs.getMainBoard();
			// 获取主板所支持的显卡接口标准
			Session sess = sessionFactory.getCurrentSession();
			int id = mb.getId();
			String sql = "FROM MB_Interface WHERE MB=" + id;
			List<MB_Interface> list = sess.createQuery(sql).list();

			List<SoundCard> returnList = new ArrayList<SoundCard>();

			for (int i = 0; i < l.size(); i++) {
				// 遍历原表进行筛选
				SoundCard nowSC = l.get(i);
				int busIntf = nowSC.getBusIntf().getId();

				for (int j = 0; j < list.size(); j++) {
					// 检测此网卡的总线接口是否符合这个主板支持的显卡接口标准
					if (busIntf == list.get(j).getIntf_id().getId()) {
						// 若支持这个系列 则加入返回list 并跳出进行下一个显卡的检验
						returnList.add(nowSC);
						break;
					} else
						continue;
				}
			}

			return returnList;
		} else
			return l;
	}

	@Override
	public List<Hardware> getNewestList() {

		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM SoundCard WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
		List<SoundCard> list = sess.createQuery(sql).list();
		if (list.size() > Constant.NEW_LIST_LENGTH)
			list = list.subList(0, Constant.NEW_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);

		return returnList;
	}

	@Override
	public List<Hardware> getHottestList() {

		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM SoundCard WHERE STATE=882";
		List<SoundCard> list = sess.createQuery(sql).list();

		Collections.sort(list);

		if (list.size() > Constant.HOT_LIST_LENGTH)
			list = list.subList(0, Constant.HOT_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);

		return returnList;
	}

	@Override
	public List<Brand> getBrandList() {

		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM BRD_SOUND";
		List<Brand> returnList = new ArrayList<Brand>();
		List<BRD_SOUND> list = sess.createQuery(sql).list();

		for (int i = 0; i < list.size(); i++) {
			returnList.add(list.get(i));
		}

		return returnList;
	}

	@Override
	public List<SoundCard> filter(int page, Map<String, List<Integer>> map,
			SortType st, SortWay sw, HardwareSet hs) {

		// 搜索时的过滤方法
		if (map == null || map.size() == 0)
			return getSoundCardList(page, st, hs, sw);

		// 获得此产品表
		List<SoundCard> hwList = getFilterList(hs, st, sw);

		// 最后的匹配表
		List<SoundCard> matchList = new ArrayList<SoundCard>();

		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.scItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.scItemNameList[1])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer.checkPrice(hwList.get(j),
								Constant.SOUNDCARD_PRICE[list.get(k) - 1]
										.getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.scItemNameList[2])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getType().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.scItemNameList[3])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getSoundChannel().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.scItemNameList[4])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getSampleSize().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<SoundCard>();
			}
		}
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);
		matchList = new Pager<SoundCard>().getPageList(matchList, minNum,
				maxNum);
		return matchList;
	}

	private List<SoundCard> getFilterList(HardwareSet hs, SortType st,
			SortWay sw) {

		/*
		 * 获取用于筛选的硬件列表 此列表可以由action提供 或由本方法从数据库获取
		 */
		List<SoundCard> l = new ArrayList<SoundCard>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = null;

		switch (st) {
		case PRICE:

			if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY OPRICE ASC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY OPRICE DESC";
			l = sess.createQuery(sql).list();

			break;
		case W_SCORE:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY WSCORE DESC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY WSCORE ASC";
			l = sess.createQuery(sql).list();

			break;
		case Z_SCORE:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY ZSCORE DESC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY ZSCORE ASC";
			l = sess.createQuery(sql).list();

			break;
		case NEW:

			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
			l = sess.createQuery(sql).list();

			break;
		case HOT:

			sql = "FROM SoundCard WHERE STATE=882";
			l = sess.createQuery(sql).list();
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				Collections.sort(l);
			else {
				Collections.sort(l, new SortByHotAsc());
			}

			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY PERFORMANCE DESC";
			else
				sql = "FROM SoundCard WHERE STATE=882 ORDER BY PERFORMANCE ASC";
			l = sess.createQuery(sql).list();
			break;
		default:
			break;

		}

		l = uniFilter(hs, l);
		return l;
	}

	@Override
	public List<SoundCard> search(String keywords, int page) {

		Session sess = sessionFactory.getCurrentSession();

		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		String sql = "FROM SoundCard WHERE STATE=882";
		String keys[] = keywords.split(" ");
		for (int i = 0; i < keys.length; i++) {
			if (i == 0) {
				sql = sql + " WHERE MODEL LIKE '%" + keys[i] + "%'";
			} else {
				sql = sql + " OR MODEL LIKE '%" + keys[i] + "%'";
			}
		}
		List<SoundCard> list = sess.createQuery(sql).list();

		List<SoundCard> returnList = new Pager<SoundCard>().getPageList(list,
				minNum, maxNum);

		return returnList;
	}

	@Override
	public HashMap<String, List<Item>> getFilterBoard() {

		HashMap<String, List<Item>> map = new HashMap<String, List<Item>>();
		map.put(getBrandItem().getItemName(), getFilterBrandItem());
		map.put(getPriceItem().getItemName(), getFilterPriceItem());

		for (int i = 2; i < Constant.scItemNameList.length; i++) {
			map.put(Constant.scItemNameList[i], getFilterItemByNum(i));
		}

		return map;
	}

	private List<Item> getFilterItemByNum(int i) {

		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		// itemName
		fi.setItemName(Constant.scItemNameList[i]);

		String sql = null;
		List<Value> values = null;
		List<Item> choices = null;

		switch (i) {
		case 2:
			// 类型

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=596 AND KEY<=600 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 3:
			// 声道

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=601 AND KEY<=610 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 4:
			// 采样尺寸

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=611 AND KEY<=620 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		default:
			break;

		}

		return choices;
	}

	private FilterItem getItemByNum(int i) {

		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		// itemName
		fi.setItemName(Constant.scItemNameList[i]);

		String sql = null;
		List<Value> values = null;
		List<String> choices = null;

		switch (i) {
		case 2:
			// 类型

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=596 AND KEY<=600 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 3:
			// 声道

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=601 AND KEY<=610 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 4:
			// 采样尺寸

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=611 AND KEY<=620 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		default:
			break;

		}
		fi.setItemChoiceList(choices);

		return fi;
	}

	private FilterItem getPriceItem() {

		// 价格项
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.scItemNameList[1]);

		List<String> choices = new ArrayList<String>();
		choices.add("300元以下");
		choices.add("300-400元");
		choices.add("400-500元");
		choices.add("500-600元");
		choices.add("600-800元");
		choices.add("800-1000元");
		choices.add("1000-1500元");
		choices.add("1500-2000元");
		choices.add("2000元以上");

		fi.setItemChoiceList(choices);

		return fi;
	}

	private FilterItem getBrandItem() {

		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.scItemNameList[0]);

		String sql = "FROM BRD_SOUND";
		List<BRD_SOUND> brandList = sess.createQuery(sql).list();
		List<String> choices = new ArrayList<String>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(brandList.get(i).getBrand());
		}

		fi.setItemChoiceList(choices);

		return fi;
	}

	@Override
	public SoundCard getOneById(int id) {

		Session sess = sessionFactory.getCurrentSession();
		SoundCard c = (SoundCard) sess.load(SoundCard.class, id);

		return c;
	}

	@Override
	public Map<String, List<String>> getInsertboard() {

		// 返回的map
		Map<String, List<String>> rMap = new HashMap<String, List<String>>();

		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<String> l = null;
		List<Value> valueList = null;

		// 品牌
		sql = "FROM BRD_SOUND";
		l = new ArrayList<String>();
		List<BRD_SOUND> brandList = sess.createQuery(sql).list();
		for (int i = 0; i < brandList.size(); i++) {
			l.add(brandList.get(i).getBrand());
		}
		rMap.put(Constant.scSelectStr[0], l);

		// Value
		// 类型
		rMap.put(Constant.scSelectStr[1], getValueByKey(596, 600));

		// 声道
		rMap.put(Constant.scSelectStr[2], getValueByKey(601, 610));

		// 采样方式
		rMap.put(Constant.scSelectStr[3], getValueByKey(611, 620));

		// Interface
		// 总线接口
		l = new ArrayList<String>();
		sql = "FROM Interface WHERE RE_SOUND=1 ORDER BY INTF_TYPE,NAME";
		List<Interface> list = sess.createQuery(sql).list();
		for (int i = 0; i < list.size(); i++) {
			l.add(list.get(i).getName());
		}
		rMap.put(Constant.scSelectStr[4], l);

		// 一对多关系
		l = new ArrayList<String>();
		sql = "FROM Interface WHERE RE_SOUND=1 ORDER BY INTF_TYPE,NAME";
		List<Value> list2 = sess.createQuery(sql).list();
		for (int i = 0; i < list2.size(); i++) {
			l.add(list2.get(i).getValue());
		}
		rMap.put(Constant.scComboBoxStr[0], l);

		return rMap;
	}

	@Override
	public boolean insertHardware(Map<String, List<String>> map) {
		try {
			// TODO Auto-generated method stub
			Session sess = sessionFactory.getCurrentSession();
			SoundCard sc = new SoundCard();
			// 本方法处理外键和专属属性，通用属性由其他方法提供
			// SOUNDCARD有一对多的情况
			Iterator i = map.keySet().iterator();
			// 一对多表
			List<String> scIntf = new ArrayList<String>();
			while (i.hasNext()) {
				String key = i.next().toString();
				List<String> list = map.get(key);
				String sql = null;
				if (key.equals("声卡型号")) {
					sc.setModel(list.get(0));
					// 外键
				} else if (key.equals("声卡品牌")) {
					sql = "FROM BRD_SOUND WHERE BRAND='" + list.get(0) + "'";
					List<BRD_SOUND> bl = sess.createQuery(sql).list();
					sc.setBrand(bl.get(0));
				} else if (key.equals("声卡类型")) {
					sc.setType(getValueByName(list.get(0)));
				} else if (key.equals("声道")) {
					sc.setSoundChannel(getValueByName(list.get(0)));
				} else if (key.equals("采样位数")) {
					sc.setSampleSize(getValueByName(list.get(0)));
				} else if (key.equals("总线接口")) {
					sc.setBusIntf(getInterfaceByName(list.get(0)));
				} else if (key.equals("声卡芯片")) {
					sc.setCpu(list.get(0));
				} else if (key.equals("支持音效")) {
					sc.setCompatibleSoundEffect(list.get(0));
				} else if (key.equals("其他")) {
					sc.setOther(list.get(0));
				} else if (key.equals("IO接口")) {
					// 非外键
					scIntf = list;
				} else {
					// 通用属性
					Changer.setUniAttribute(sc, key, list);
				}

			}
			Changer.setDefaultAttribute(sc);
			sc.setState(getValueByID(Constant.HW_DEFAULT_STATE));
			// 最后填充性能
			sc.setPerformance(new SoundCardEvaluator().getPerformance(sc));
			// Transaction t = sess.beginTransaction();
			sess.save(sc);
			// io接口表SC_IOINTF
			for (int j = 0; j < scIntf.size(); j++) {
				String s = scIntf.get(j);
				Interface v = getInterfaceByName(s);

				SC_IOINTF scif = new SC_IOINTF();
				scif.setSc(sc);
				scif.setIntf(v);
				sess.save(scif);
			}
			sess.flush();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private List<String> getValueByKey(int i, int j) {

		List<String> rl = new ArrayList<String>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value where KEY>=" + i + " AND KEY<=" + j
				+ " ORDER BY VALUE";
		List<Value> l = sess.createQuery(sql).list();
		for (int k = 0; k < l.size(); k++) {
			rl.add(l.get(k).getValue());
		}

		return rl;
	}

	private Value getValueByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value WHERE VALUE='" + name + "'";
		List<Value> l = sess.createQuery(sql).list();

		return l.get(0);

	}

	private Interface getInterfaceByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Interface WHERE NAME='" + name + "'";
		List<Interface> l = sess.createQuery(sql).list();

		return l.get(0);

	}

	private List<Item> getFilterPriceItem() {

		List<Item> choices = new ArrayList<Item>();
		for (int i = 0; i < (Constant.SOUNDCARD_PRICE.length); i++) {
			choices.add(Constant.SOUNDCARD_PRICE[i]);
		}
		return choices;
	}

	private List<Item> getFilterBrandItem() {

		Session sess = sessionFactory.getCurrentSession();

		String sql = "FROM BRD_SOUND";
		List<BRD_SOUND> brandList = sess.createQuery(sql).list();
		List<Item> choices = new ArrayList<Item>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(new Item(brandList.get(i).getBrand(), brandList.get(i)
					.getId()));
		}

		return choices;
	}

	private Value getValueByID(Integer integer) {

		Session sess = sessionFactory.getCurrentSession();
		Value c = (Value) sess.load(Value.class, integer);

		return c;
	}

	private Interface getInterfaceByID(int id) {
		Session sess = sessionFactory.getCurrentSession();
		Interface c = (Interface) sess.load(Interface.class, id);

		return c;

	}

	private BRD_SOUND getBrandById(Integer integer) {

		Session sess = sessionFactory.getCurrentSession();
		BRD_SOUND c = (BRD_SOUND) sess.load(BRD_SOUND.class, integer);

		return c;
	}

	@Override
	public void save(SoundCard t) {

		Session sess = sessionFactory.getCurrentSession();
		// Transaction tran = sess.beginTransaction();
		sess.saveOrUpdate(t);
		sess.flush();

	}

	@Override
	public int filterPageNum(Map<String, List<Integer>> map, HardwareSet hs) {

		List<SoundCard> hwList = null;
		List<SoundCard> matchList = new ArrayList<SoundCard>();

		String sql = "FROM SoundCard WHERE STATE=882";
		Session sess = sessionFactory.getCurrentSession();
		hwList = sess.createQuery(sql).list();
		
		hwList = uniFilter(hs,hwList);

		if (map == null || map.size() == 0)
			return hwList.size();
		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.scItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.scItemNameList[1])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer.checkPrice(hwList.get(j),
								Constant.SOUNDCARD_PRICE[list.get(k) - 1]
										.getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.scItemNameList[2])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getType().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.scItemNameList[3])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getSoundChannel().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.scItemNameList[4])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getSampleSize().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<SoundCard>();
			}
		}
		return matchList.size();

	}

	public int getListPageNum(HardwareSet hs) {

		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM SoundCard WHERE STATE=882";
		List<SoundCard> list = sess.createQuery(sql).list();
		list = uniFilter(hs, list);
		return list.size();
	}

	private List<SoundCard> uniFilter(HardwareSet hs, List<SoundCard> list) {
		if (hs != null) {
			list = MainBoardfilter(hs, list);
		}
		return list;
	}

	@Override
	public List<SoundCard> getOneBySupplierID(int id) {
		// TODO Auto-generated method stub
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM SoundCard where SUPPLIER="+id;
		List<SoundCard> list = sess.createQuery(sql).list();
		
		return list;
	}
}
